% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This file is a template using the "beamer" package to create slides for a talk or presentation
% - Talk at a conference/colloquium.
% - Talk length is about 20min.
% - Style is ornate.

% MODIFIED by Jonathan Kew, 2008-07-06
% The header comments and encoding in this file were modified for inclusion with TeXworks.
% The content is otherwise unchanged from the original distributed with the beamer package.

\documentclass{beamer}


% Copyright 2004 by Till Tantau <tantau@users.sourceforge.net>.
%
% In principle, this file can be redistributed and/or modified under
% the terms of the GNU Public License, version 2.
%
% However, this file is supposed to be a template to be modified
% for your own needs. For this reason, if you use this file as a
% template and not specifically distribute it as part of a another
% package/program, I grant the extra permission to freely copy and
% modify this file as you see fit and even to delete this copyright
% notice. 


\mode<presentation>
{
  \usetheme{Warsaw}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}


\usepackage[english]{babel}
% or whatever

\usepackage[utf8]{inputenc}
% or whatever

\usepackage{times}
\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.
\renewcommand{\raggedright}{\leftskip=0pt \rightskip=0pt plus 0cm}
\raggedright



\title[Transaction in MongoDB 4.x] % (optional, use only with long paper titles)
{Transaction in MongoDB 4.x}


\author[Zhilei Ren] % (optional, use only with lots of authors)
{Zhilei Ren}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
%   affiliation.

% This is only inserted into the PDF information catalog. Can be left
% out. 



% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

% \pgfdeclareimage[height=0.5cm]{university-logo}{university-logo-filename}
% \logo{\pgfuseimage{university-logo}}



% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 

%\beamerdefaultoverlayspecification{<+->}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}



% Structuring a talk is a difficult task and the following structure
% may not be suitable. Here are some rules that apply for this
% solution: 

% - Exactly two or three sections (other than the summary).
% - At *most* three subsections per section.
% - Talk about 30s to 2min per frame. So there should be between about
%   15 and 30 frames, all told.

% - A conference audience is likely to know very little of what you
%   are going to talk about. So *simplify*!
% - In a 20min talk, getting the main ideas across is hard
%   enough. Leave out details, even if it means being less precise than
%   you think necessary.
% - If you omit details that are vital to the proof/implementation,
%   just say so once. Everybody will be happy with that.

\begin{frame}
	\begin{block}{Background}
In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.
	\end{block}
\end{frame}

\begin{frame}
	\begin{block}{Background}
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.
	\end{block}
\end{frame}

\begin{frame}[t]

Multi-document transactions are atomic (i.e. provide an "all-or-nothing" proposition):
\begin{block}{}
    \begin{enumerate}
        \item When a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. That is, a transaction will not commit some of its changes while rolling back others.

        \item Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.

        \item However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern "local" can read the results of write 1 without seeing write 2.

        \item When a transaction aborts, all data changes made in the transaction are discarded without ever becoming visible. For example, if any operation in the transaction fails, the transaction aborts and all data changes made in the transaction are discarded without ever becoming visible.
    \end{enumerate}

\end{block}
\end{frame}

\begin{frame}


For transactions:

\begin{enumerate}
    \item You can specify read/write (CRUD) operations on existing collections. The collections can be in different databases. 
    \item You cannot write to capped collections. (Starting in MongoDB 4.2)
    \item You cannot read/write to collections in the config, admin, or local databases.
    \item You cannot write to system.* collections.
    \item You cannot return the supported operation's query plan (i.e. explain).
    \item For cursors created in a transaction, you cannot call getMore outside the transaction.
    \item Starting in MongoDB 4.2, you cannot specify killCursors as the first operation in a transaction.
\end{enumerate}
\end{frame}
\begin{frame}[t]

Transactions and Sessions
\begin{enumerate}
    \item Transactions are associated with a session; i.e. you start a transaction for a session.
    \item At any given time, you can have at most one open transaction for a session.
    \item When using the drivers, each operation in the transaction must be associated with the session. Refer to your driver specific documentation for details.
    \item If a session ends and it has an open transaction, the transaction aborts.
\end{enumerate}



    
\end{frame}

\begin{frame}[fragile] 
    \scriptsize
    \begin{verbatim} 
// Prereq: Create collections. CRUD operations in transactions 
// must be on existing collections.
db.getSiblingDB("mydb1").foo.insert({abc: 0}, 
                     {writeConcern: {w: "majority", wtimeout: 2000}});
db.getSiblingDB("mydb2").bar.insert({xyz: 0}, 
                     {writeConcern: {w: "majority", wtimeout: 2000}});

// Start a session.
session = db.getMongo().startSession({readPreference: 
                     {mode: "primary"}});

coll1 = session.getDatabase("mydb1").foo;
coll2 = session.getDatabase("mydb2").bar;
// Start a transaction
session.startTransaction({readConcern: {level: "local"}, 
                     writeConcern: {w: "majority"}});
\end{verbatim}
\end{frame}

\begin{frame}[fragile] 
    \scriptsize
    \begin{verbatim} 

// Operations inside the transaction
try {
   coll1.insertOne({abc: 1});
   coll2.insertOne({xyz: 999});
} catch (error) {
   // Abort transaction on error
   session.abortTransaction();
   throw error;
}

// Commit the transaction using write concern 
// set at transaction start
session.commitTransaction();

session.endSession();
\end{verbatim}
\end{frame}

\end{document}


